package util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

public class PropertiesUtil {

	private static Properties props;

	// Tomcat运行时执行
	// 代码块执行顺序：静态代码块>普通代码块>构造代码块
	// 构造代码块每次都执行，但是静态代码块只执行一次
	static {
		String fileName = "jdbc.properties";
		props = new Properties();
		try {
			props.load(new InputStreamReader(PropertiesUtil.class.getResourceAsStream(fileName), "utf-8"));
//			props.load(PropertiesUtil.class.getResourceAsStream(fileName));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getProperty(String key) {
		String value = props.getProperty(key.trim());
		return value.trim();
	}

	public static String getProperty(String key, String defaultValue) {
		String value = getProperty(key);
		if (value != null) {
			return value;
		}
		return defaultValue;
	}
	public static void main(String[] args) {
		System.out.println(getProperty("name"));
		System.out.println(getProperty("user"));
		System.out.println(getProperty("url"));
	}
}